import {
  goodsTagList,
  goodsInfoList
} from '@/api/shop'
import {
  bannerList
} from '@/api/home'

Page({

  data: {
    imgUri: getApp().globalData.imgUri,
    cdn: getApp().globalData.cdn,

    pick_list: [],
    swiperIndex: 0,
    navTab: [],
    navIndex: 0,
    gtId: '', // 标签id 
    isEnd: false,
    pageSize: 10,
    shopList: []
  },
  onLoad() {
    let t = this;
    t.getTagList();

  },
  // 获取标签
  getTagList() {
    let t = this;
    goodsTagList({storeType:2}).then(res => {
      if (res.code == 200) {
        t.setData({
          navTab: res.rows,
          gtId: res.rows[0].gtId
        });
        t.getShopList();

      }
    });
  },
  // 获取商品列表
  getShopList(pageNum = 1) {
    let t = this;
    let o = {
      pageNum,
      pageSize: t.data.pageSize,
      gtId: t.data.gtId,
      status: 0
    }
    goodsInfoList(o).then((res) => {
      if (res.code == 200) {
        for (var i = 0; i < res.rows.length; i++) {
          res.rows[i].collect = false
        }
        let list = t.data.shopList.concat(res.rows)
        let isEnd = list.length >= res.total
        t.setData({
          shopList: list,
          isEnd,
        });
        t.getBannerList();
      }
    })
  },
  // 获取首页banner
  getBannerList() {
    let t = this;
    let phone = wx.getStorageSync('phone')
    let o = {
      moduleType: 'GEMO_VGOODS',
      status: 0
    }
    if (phone) {
      o.gemoLevel = wx.getStorageSync('memberLevel').currentLevelEnum
      o.haveCommonBanner = true
    } else {
      o.onlyCommonBanner = true
    }
    bannerList(o).then(res => {
      if (res.code == 200) {
        t.setData({
          pick_list: res.rows
        });
      }
    })
  },
  bindscrolltolower() {
    console.log('bindscrolltolower')
    let {
      isEnd
    } = this.data
    if (!isEnd) {
      let pageNum = Math.ceil(this.data.shopList.length / this.data.pageSize) + 1
      this.getShopList(pageNum)
    }
  },
  swiperChange(e) {
    this.setData({
      swiperIndex: e.detail.current
    })
  },
  changeNav(e) {
    let {
      index
    } = e.currentTarget.dataset
    let item = this.data.navTab[index]
    this.setData({
      shopList:[],
      navIndex: index,
      gtId: item.gtId
    })
    this.getShopList()
  },
  goToBannerDetail(e){
    let {
      item
    } = e.currentTarget.dataset;
    if (item.jumpType != "NONE"){
      wx.setStorageSync('gemoPickItem', item)
      wx.navigateTo({
        url: '/pages/gemoPickDetail/gemoPickDetail',
      });
    }
  },
  goToDetail(e) {
    let {
      agiid
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/shop/detail/detail?id=' + agiid+'&source=2',
    })
  },
  onShareAppMessage() {
    return {
      title: getApp().globalData.share_title,
      path: getApp().globalData.share_url,
      imageUrl: getApp().globalData.share_image,
    }
  }
})